﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Common.BLL;

public partial class Police_TransferSoldier1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101") ||
                Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102"))
            {

                if (!string.IsNullOrEmpty(Request["ID"]))
                {

                    string sid = Request["ID"];
                    Soldier soldier = new Soldier();
                    SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(sid);
                    BindSoldier(soldierInfo);
                    UserInfo member = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
                    //OldDepartmentList.DataSource = GetDepartmentList(member.DepartmentID, "203001", "203002", "203003");
                    //OldDepartmentList.DataTextField = "FullName";
                    //OldDepartmentList.DataValueField = "DepartmentID";
                    //OldDepartmentList.DataBind();
                    //if (soldierInfo.DepartmentID != "")
                    //    OldDepartmentList.SelectedValue = soldierInfo.DepartmentID;
                    lblOldDepartmentID.Text = soldierInfo.DepartmentID;
                    lblOldDepartmentName.Text = soldierInfo.DepartmentName;
                    //内部调动部门
                    DepartmentList.DataSource = GetDepartmentList(member.DepartmentID, "203001", "203002", "203003");
                    DepartmentList.DataTextField = "FullName";
                    DepartmentList.DataValueField = "DepartmentID";
                    DepartmentList.DataBind();

                    tyType.DataSource = CodeItem.GetList("parentID='SBDD40' order by seqID");
                    tyType.DataTextField = "ItemName";
                    tyType.DataValueField = "ItemID";
                    tyType.DataBind();
                    tyType.SelectedIndex = 0;

                    txtTransfeDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtReportDate.Text = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
                 
                    SetPanel();
                }
            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }

    }

    public string GetSoldierID()
    {
        return Request.QueryString["ID"];
    }

    private void BindSoldier(SoldierArchiveInfo soldierInfo)
    {

        SoldierName.Text = soldierInfo.SoldierName;
        CardID.Text = soldierInfo.CardID;
        Sex.Text = soldierInfo.Sex;
        imgPhoto.ImageUrl = soldierInfo.Picture == "" ? "~/Images/NoPhoto.png" : "../../" + soldierInfo.Picture;
        DepartmentName.Text = soldierInfo.DepartmentName;
        NationName.Text = soldierInfo.NativeName;
        Birthday.Text = soldierInfo.Birthday == DateTime.MinValue ? "--" : soldierInfo.Birthday.ToString("yyyy-MM-dd");
        JoinDate.Text = soldierInfo.JoinTime == DateTime.MinValue ? "--" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
        RankName.Text = soldierInfo.RankName;
        DutyName.Text = soldierInfo.DutyName;
        SpecialityName.Text = soldierInfo.Speciality2 == "" ? soldierInfo.Speciality1Name : soldierInfo.Speciality1Name + "[" + soldierInfo.Speciality2Name + "]";
        //hlSoldierDetails.NavigateUrl = "~/BasicBase/SoldierArchive/SoldierView.aspx?RootMenuID=200000&ID=" + soldierInfo.SoldierID;
        //hlSoldierDetails.Target = "_blank";
    }



    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
            departmentList = Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100') or (DepartmentID = '1300017') or (DepartmentID = '1300038')or (DepartmentID = '1300035')");
        else

            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel))
                departmentList = Department.GetList("DepartmentID like '" + departmentID.Substring(0, 4) + "%'");
            else
                if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
                    departmentList = Department.GetList("DepartmentID='" + departmentID + "'");

        return departmentList;
    }

    public List<DepartmentInfo> GetDepartmentList()
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        departmentList = Department.GetList("Right(DepartmentID,2)='00'");
        return departmentList;
    }
    //内调
    protected void btnTransfer_Click(object sender, EventArgs e)
    {
        string transferID = "";
        if (!String.IsNullOrEmpty(Request["TID"]))
            transferID = Request["TID"];
        if (TransferSoldier.Transfer(Request["ID"], lblOldDepartmentID.Text, lblOldDepartmentName.Text, DepartmentList.SelectedValue,
            DepartmentList.SelectedItem.Text, Convert.ToDateTime(txtTransfeDate.Text), User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName,
            DateTime.Now, "SBDD30", "SBDD30", Convert.ToDateTime(txtReportDate.Text), txtApprover.Text, transferID)!=Guid.Empty)
        {
            Soldier soldier = new Soldier();
            SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(Request["ID"]);
            soldierInfo.DepartmentID = DepartmentList.SelectedValue;
            soldier.Update(soldierInfo, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName);
            lblInfo.Text = "调动成功。";
         
        }
        else
            lblInfo.Text = "调动失败！";
    }

   
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        SetPanel();
    }

    private void SetPanel()
    {
        this.Panel1.Visible = this.RadioButton1.Checked;
 
        this.Panel4.Visible = this.RadioButton4.Checked;
    }

    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
    }
    protected void btnTransOut1_Click(object sender, EventArgs e)
    {
        string transferID = "";
        if (!String.IsNullOrEmpty(Request["TID"]))
            transferID = Request["TID"];
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(Request["ID"]);
        if (TransferSoldier.Transfer(Request["ID"], soldierInfo.DepartmentID, soldierInfo.DepartmentName, "", tyType.SelectedItem.Text,
            Convert.ToDateTime(txtTransfeDate.Text), User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName,
            DateTime.Now, "SBDD40", tyType.SelectedValue, Convert.ToDateTime(txtReportDate.Text), txtApprover.Text, transferID) != Guid.Empty)
        {

            soldier.Delete(Request["ID"], "退役", DateTime.Now, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName);
            lblInfo.Text = "调出成功。";
  
        }
        else
            lblInfo.Text = "调出失败！";
    }
}
